package id.co.rho.scoring.cid.server.command.handler.impl;

import id.co.rho.scoring.cid.client.command.impl.LoginCommandAction;
import id.co.rho.scoring.cid.client.command.impl.LoginCommandResult;
import id.co.rho.scoring.cid.server.command.handler.CommandHandler;
import id.co.rho.scoring.cid.server.service.LoginService;
import id.co.rho.scoring.cid.server.util.AppContext;

import org.springframework.context.ApplicationContext;

public class LoginCommandHandler implements CommandHandler<LoginCommandAction, LoginCommandResult> {

    @Override
    public LoginCommandResult execute(
            LoginCommandAction command) {
        ApplicationContext ctx = AppContext.getApplicationContext();
        LoginService bean = ctx.getBean(LoginService.class);
        switch (command.getType()) {
            case FIRSTLOAD:
                return bean.validateSessionId(command.getStringCockie());
            case LOGIN:
                return bean.validateLogin(command.getUsername(), command.getPassword());
            case LOGOUT:
                return bean.logout(command.getStringCockie());
        }
        return null;
    }

    @Override
    public Class<LoginCommandAction> getActionType() {
        return LoginCommandAction.class;
    }

}
